import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { ElMessage } from 'element-plus'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue')
    },



    {
      path: '/',
      name: 'home',
      component: HomeView,
      children:[
        {
          path: '/',
          name: 'index',
          component: () => import('../views/Index.vue')
        },


        {
          path: '/music',
          name: 'music',
          component: () => import('../views/AboutView.vue')
        },
        {
          path: '/test',
          name: 'test',
          component: () => import('../components/WelcomeItem.vue')
        },
        {
          path: '/test1',
          name: 'test1',
          component: () => import('../components/TheWelcome.vue')
        }



      ]
    },
    {
      path: '/:catchAll(.*)',
      name: 'my404',
      component: () => import('../views/My404.vue')
    }
   
  ]
})

/**
 * 路由守卫 ，前置
 */
router.beforeEach((to, from, next)=>{
    //判断是否是登录  
    if(to.path==='/login'){
       next()  
    }else{

      const  tokenObject  = localStorage.getItem("token")
      if(tokenObject){
          next()    

      }else{
        ElMessage.error("亲，你还没有登录")
        next("/login")
      }
  
    }

});

export default router
